package com.ruoyi.web.controller.app;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import com.ruoyi.web.controller.tool.EwmUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公告信息Controller
 * 
 * @author ruoyi
 * @date 2024-02-05
 */
@RestController
@RequestMapping("/stapp/order")
public class StOrderAppController extends BaseController
{
    @Autowired
    private IStOrderService stOrderService;
    @Autowired
    private IStVerificationQrCodeService stVerificationQrCodeService;
    @Autowired
    private EwmUtils ewmUtils;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IStOrderDetailService stOrderDetailService;
    @Autowired
    private IStFoodService stFoodService;
    @Autowired
    private IStSysDicService stSysDicService;
    @Autowired
    private IStUserOpeService stUserOpeService;
    //获取食堂简介
    @PostMapping("orderlist")
    HashMap<Object, Object> orderlist(@RequestBody StOrder stOrder) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);

        List<StOrder> StOrderlist = stOrderService.selectStOrderList(stOrder);
        aa.put("requestdata",StOrderlist);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("orderidstates")
    HashMap<Object, Object> orderidstates(@RequestBody StOrder stOrder) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);

        StOrder stOrdernew = stOrderService.selectStOrderById(stOrder.getId());
        aa.put("requestdata",stOrdernew);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("verificationidstates")
    HashMap<Object, Object> verificationidstates(@RequestBody StOrder stOrder) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);

        StVerificationQrCode stOrdernew = stVerificationQrCodeService.selectStVerificationQrCodeById(stOrder.getId());
        aa.put("requestdata",stOrdernew);
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
  /*  核销二维码*/
    @GetMapping("orderuse")
    HashMap<Object, Object> orderuse(String orderid,String serial) {
        if(serial==null){
            serial="";
        }
        System.out.println(">>>>>>设备序列号"+serial);
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();

        StOrder stOrder =stOrderService.selectStOrderById(orderid);
        if(stOrder.getName().equals("熟食")&&stOrder.getStates().equals("1")){
            stOrder.setStates("2");
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
            String newsNo = LocalDateTime.now().format(fmt);
            StVerificationQrCode stVerificationQrCode = new StVerificationQrCode();
            stVerificationQrCode.setName("熟食核销");
            stVerificationQrCode.setSerial(serial);
            stVerificationQrCode.setPhone(stOrder.getUserphone());
            stVerificationQrCode.setUserName(stOrder.getUser());
            stVerificationQrCode.setUser(stOrder.getUsername());
            stVerificationQrCode.setPrice(stOrder.getAmount());
            stVerificationQrCode.setVerificationTime(DateUtils.getTime());
            stVerificationQrCode.setId(newsNo);
            stVerificationQrCode.setStates("核销成功");
            stVerificationQrCode.setOrderid(stOrder.getId());
           /* 判断余额*/
            SysUser suser = userService.selectUserByPhone(stOrder.getUserphone());
            if(suser.getBalance()<Double.parseDouble(stOrder.getAmount())){
                map.put("code",-1);
                map.put("msg","余额不够，核销失败");
                return map;
            }
            stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
            stOrderService.updateStOrder(stOrder);
            /*扣款调整成预定熟食时候就扣款*/
          /* suser.setBalance(suser.getBalance()-Double.parseDouble(stOrder.getAmount()));
            userService.updateUser(suser);*/
            aa.put("code",0);
            aa.put("requestdata",suser);
            aa.put("msg","核销成功"+suser.getNickName()+"你本次消费金额为"+Double.parseDouble(stOrder.getAmount())+"元");
            map.put("data",aa);
            return map;
        }else if(stOrder.getStates().equals("1")){
            stOrder.setStates("2");
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
            String newsNo = LocalDateTime.now().format(fmt);
            StVerificationQrCode stVerificationQrCode = new StVerificationQrCode();
            stVerificationQrCode.setName("午餐核销");
            stVerificationQrCode.setSerial(serial);
            stVerificationQrCode.setPhone(stOrder.getUserphone());
            stVerificationQrCode.setUserName(stOrder.getUser());
            stVerificationQrCode.setUser(stOrder.getUsername());
            stVerificationQrCode.setPrice("5.0");
            stVerificationQrCode.setVerificationTime(DateUtils.getTime());
            stVerificationQrCode.setId(newsNo);
            stVerificationQrCode.setStates("核销成功");
            stVerificationQrCode.setOrderid(stOrder.getId());
            /* 判断余额*/
            SysUser suser = userService.selectUserByPhone(stOrder.getUserphone());
            if(suser.getBalance()<Double.parseDouble("5.0")){
                map.put("code",-1);
                map.put("msg","余额不够，核销失败");
                return map;
            }
            stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
            stOrderService.updateStOrder(stOrder);
            /*扣款*/
            suser.setBalance(suser.getBalance()-Double.parseDouble("5.0"));
            userService.updateUser(suser);
            aa.put("code",0);
            aa.put("requestdata",suser);
            aa.put("msg","核销成功"+suser.getNickName()+"你本次消费金额为"+Double.parseDouble("5.0")+"元");
            map.put("data",aa);
            return map;

        }else{
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
            String newsNo = LocalDateTime.now().format(fmt);
            StVerificationQrCode stVerificationQrCode = new StVerificationQrCode();
            stVerificationQrCode.setName("午餐核销");
            stVerificationQrCode.setPhone(stOrder.getUserphone());
            stVerificationQrCode.setUserName(stOrder.getUser());
            stVerificationQrCode.setUser(stOrder.getUsername());
            stVerificationQrCode.setPrice("5.0");
            stVerificationQrCode.setSerial(serial);
            stVerificationQrCode.setVerificationTime(DateUtils.getTime());
            stVerificationQrCode.setId(newsNo);
            stVerificationQrCode.setStates("该订单已消费");
            stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
            aa.put("code",-1);
            aa.put("requestdata","");
            aa.put("msg","该订单已消费");
            map.put("data",aa);
            return map;
        }
    }
    @GetMapping("orderdel")
    HashMap<Object, Object> ordercancel(String orderid) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);
        StOrder stOrder =stOrderService.selectStOrderById(orderid);
        stOrder.setStates("3");
        stOrderService.updateStOrder(stOrder);
        System.out.println(">>>>>>>>>>>>>>>"+orderid);
        /*StOrder StOrder = new StOrder();
        List<StOrder> StOrderlist = stOrderService.selectStOrderList(StOrder);*/
        aa.put("requestdata","");
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("ordering")
    HashMap<Object, Object> ordering(@RequestBody StOrder stOrder) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        aa.put("code",0);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
        String newsNo = LocalDateTime.now().format(fmt);
        // 扫码跳转地址
        String address = "http://124.71.210.36:9080/stapp/order/orderuse?orderid="+newsNo;
        // 生成二维码的图片类型
        String imageType = "PNG";
        // 调用生成二维码方法
        String ewmInfo = ewmUtils.generateBase64(address, imageType);
        stOrder.setQRcode(ewmInfo);
        stOrder.setStates("1");
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("hhmmssS");
        String a= stOrder.getTime().replaceAll("-","")+dateFormat.format(date);
        if(stOrder.getName().equals("午餐")){
            stOrder.setAmount("5.0");
             stOrder.setImageurl("http://124.71.210.36:9080/profile/upload/2024/07/04/%E5%8D%88%E9%A4%90.png");
        }else{
            stOrder.setAmount("5.0");
            stOrder.setImageurl("http://124.71.210.36:9080/profile/upload/2024/07/04/%E9%B2%9C%E8%82%89%E5%8C%85.png");
        }
        stOrder.setPickupcode(a);
        stOrder.setPickupadd("左侧食堂充值点");
        stOrder.setId(newsNo);
        SysUser sysUser = userService.selectUserByUserName(stOrder.getUser());
        Double YYtotalAmount = stOrderService.selectcountamountStOrder(sysUser.getUserName());
        if(sysUser.getBalance()<(Double.parseDouble("5.0")+YYtotalAmount)){
            map.put("code",-1);
            map.put("msg","余额不够，订购失败");
            return map;
        }
        stOrderService.insertStOrder(stOrder);
        aa.put("requestdata","");
        aa.put("msg","提交成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("Verification_QR_code")
    String Verification_QR_code(@RequestBody Map parammap) {

        HashMap<Object, Object> map = new HashMap<>();
        System.out.println(parammap.get("QRcode"));
        String aa = HttpUtils.sendGet(parammap.get("QRcode").toString());
        System.out.println(aa);

        return aa;
    }
    @PostMapping("/verificationlist")
    public TableDataInfo verificationlist( StVerificationQrCode stVerificationQrCode)
    {
        startPage();
        List<StVerificationQrCode> list = stVerificationQrCodeService.selectStVerificationQrCodeList(stVerificationQrCode);
        return getDataTable(list);
    }
    @PostMapping("/verificationlistapp")
    public HashMap<Object, Object> verificationlistapp(@RequestBody StVerificationQrCode stVerificationQrCode)
    {
        HashMap<Object, Object> map = new HashMap<>();
        List<StVerificationQrCode> list = stVerificationQrCodeService.selectStVerificationQrCodeList(stVerificationQrCode);
        map.put("data",list);
        return map;
    }
    @PostMapping("/verificationliststatistics")
    public HashMap<Object, Object> verificationliststatistics(@RequestBody StVerificationQrCodestatistics stVerificationQrCodestatistics)
    {
        HashMap<Object, Object> map = new HashMap<>();
        List<StVerificationQrCode> list = stVerificationQrCodeService.selectStVerificationQrCodeListstatistics(stVerificationQrCodestatistics);
        map.put("data",list);
        return map;
    }
    @PostMapping("Verification_QR_code_userinfo")
    HashMap<Object, Object> Verification_QR_code_userinfo(@RequestBody Map parammap ) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        System.out.println(parammap.get("QRcode"));
        String bb = parammap.get("QRcode").toString();
        String price = parammap.get("price").toString();
        if(price.equals("")||price.equals("0")){
            aa.put("code",-1);
            aa.put("requestdata","");
            aa.put("msg","核销失败");
            map.put("data",aa);
            return map;
        }else{
        JSONObject maps = JSONObject.parseObject(bb);
        JSONObject userinfo = JSONObject.parseObject(maps.getString("myuserinfo"));
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
        String newsNo = LocalDateTime.now().format(fmt);
        StVerificationQrCode stVerificationQrCode = new StVerificationQrCode();
        stVerificationQrCode.setName("早餐核销");
        stVerificationQrCode.setPhone(userinfo.get("phonenumber").toString());
        stVerificationQrCode.setUserName(userinfo.get("userName").toString());
        stVerificationQrCode.setPrice(price);
        SysUser suser = userService.selectUserByPhone(userinfo.get("phonenumber").toString());
        stVerificationQrCode.setUser(suser.getNickName());
        stVerificationQrCode.setVerificationTime(DateUtils.getTime());
        stVerificationQrCode.setId(userinfo.get("qrcodeid").toString());
        stVerificationQrCode.setStates("核销成功");
        if(suser.getBalance()<Double.parseDouble(price)){
            stVerificationQrCode.setStates("核销失败,余额不够");
            stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
                map.put("code",-1);
                map.put("msg","余额不够，付款失败");
                return map;
        }
        stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
        /*扣款*/
        suser.setBalance(suser.getBalance()-Double.parseDouble(price));
        userService.updateUser(suser);

        aa.put("code",0);
        aa.put("requestdata",suser);
        aa.put("msg","核销成功");
        map.put("data",aa);

        return map;
        }
    }
    @PostMapping("Verification_QR_code_userinfo_android")
    HashMap<Object, Object> Verification_QR_code_userinfo_android(@RequestBody Map parammap ,String price,String serial ) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        System.out.println(">>>>>>"+price);
        if(serial==null){
            serial="";
        }
        System.out.println(">>>>>>设备序列号"+serial);


        JSONObject userinfo = JSONObject.parseObject(JSON.toJSONString(parammap.get("myuserinfo")));
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
        String newsNo = LocalDateTime.now().format(fmt);

        /*判断当前时间是否属于就餐时间*/
        StSysDic stSysDic=new StSysDic();
        List<StSysDic> stSysDiclist = stSysDicService.selectStSysDicList(stSysDic);
        boolean falg = false;
        System.out.println(">>>>>>>>>>>>>>>"+newsNo.substring(8,14));
        for(int i=0;i<stSysDiclist.size();i++){
            if(newsNo.substring(8,14).compareTo(stSysDiclist.get(i).getStarttime())>0&&newsNo.substring(8,14).compareTo(stSysDiclist.get(i).getEndtime())<0){
                falg=true;
            }
        }
        //手持设备跳过时间校验
        if(serial.equals("000000P8")){
            falg=true;
        }
        if(!falg){
            aa.put("code",-1);
            aa.put("msg","当前不属于就餐时间，请稍后再来");
            map.put("data",aa);
            return map;
        }
        /*判断时间磋*/
        if((Long.parseLong(newsNo)-Long.parseLong(userinfo.get("qrcodeid").toString()))>500000){
            aa.put("code",-1);
            aa.put("msg","该二维码已失效,请刷新二维码");
            map.put("data",aa);
            return map;
        }
        StVerificationQrCode stVerificationQrCode = new StVerificationQrCode();
        stVerificationQrCode.setSerial(serial);
        stVerificationQrCode.setPhone(userinfo.get("phonenumber").toString());
        stVerificationQrCode.setUserName(userinfo.get("userName").toString());

        SysUser suser = userService.selectUserByPhone(userinfo.get("phonenumber").toString());
        stVerificationQrCode.setUser(suser.getNickName());
        stVerificationQrCode.setVerificationTime(DateUtils.getTime());
        stVerificationQrCode.setId(userinfo.get("qrcodeid").toString());
        stVerificationQrCode.setStates("核销成功");
        /*查询该用户今日是否已用餐*/
        StVerificationQrCode userinfoVerificationQrCode = new StVerificationQrCode();
        userinfoVerificationQrCode.setUserName(userinfo.get("userName").toString());
        //手持设备跳过
        if(serial.equals("000000P8")){
            stVerificationQrCode.setName("自助消费");
        }else {
            if (newsNo.substring(8, 14).compareTo(stSysDicService.selectStSysDicById(1L).getStarttime()) > 0 && newsNo.substring(8, 14).compareTo(stSysDicService.selectStSysDicById(1L).getEndtime()) < 0) {
                System.out.println(">>>>>>>>>>>>>>>早餐核销" + newsNo.substring(8, 14));
                stVerificationQrCode.setName("早餐核销");
            }
            if (newsNo.substring(8, 14).compareTo(stSysDicService.selectStSysDicById(2L).getStarttime()) > 0 && newsNo.substring(8, 14).compareTo(stSysDicService.selectStSysDicById(2L).getEndtime()) < 0) {
                System.out.println(">>>>>>>>>>>>>>>午餐核销" + newsNo.substring(8, 14));
                stVerificationQrCode.setName("午餐核销");
                /*查询今日是否已就餐*/
                userinfoVerificationQrCode.setName("午餐核销");
                userinfoVerificationQrCode.setVerificationTime(DateUtils.getTime().substring(0, 10));
                System.out.println(">>>>>>>>>>>>>>>午餐核销" + DateUtils.getTime().substring(0, 10));
                List<StVerificationQrCode> userinfoVerificationQrCodeList = stVerificationQrCodeService.selectStVerificationQrCodeList(userinfoVerificationQrCode);
                if (userinfoVerificationQrCodeList.size() == 0) {
                    price = "5.0";
                } else {
                    price = "15.0";
                }
            }
            if (newsNo.substring(8, 14).compareTo(stSysDicService.selectStSysDicById(3L).getStarttime()) > 0 && newsNo.substring(8, 14).compareTo(stSysDicService.selectStSysDicById(3L).getEndtime()) < 0) {
                System.out.println(">>>>>>>>>>>>>>>晚餐核销" + newsNo.substring(8, 14));
                stVerificationQrCode.setName("晚餐核销");
                /*查询今日是否已就餐*/
                userinfoVerificationQrCode.setName("晚餐核销");
                userinfoVerificationQrCode.setVerificationTime(DateUtils.getTime().substring(0, 10));
                List<StVerificationQrCode> userinfoVerificationQrCodeList = stVerificationQrCodeService.selectStVerificationQrCodeList(userinfoVerificationQrCode);
                if (userinfoVerificationQrCodeList.size() == 0) {
                    price = "5.0";
                } else {
                    price = "10.0";
                }


            }
            if (newsNo.substring(8, 14).compareTo(stSysDicService.selectStSysDicById(4L).getStarttime()) > 0 && newsNo.substring(8, 14).compareTo(stSysDicService.selectStSysDicById(4L).getEndtime()) < 0) {
                System.out.println(">>>>>>>>>>>>>>>宵夜核销" + newsNo.substring(8, 14));
                stVerificationQrCode.setName("宵夜核销");
            }
        }
        stVerificationQrCode.setPrice(price);
        if(Double.parseDouble(price)==0){
            stVerificationQrCode.setStates("核销失败,消费金额为0.0");
            stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
            aa.put("code",-1);
            aa.put("msg","核销失败,消费金额为0.0");
            map.put("data",aa);
            return map;
        }else if(suser.getBalance()<Double.parseDouble(price)){
            stVerificationQrCode.setStates("核销失败,余额不够");
            stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
            aa.put("code",-1);
            aa.put("msg","余额不够，付款失败");
            map.put("data",aa);
            return map;
        }
        stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
        /*扣款*/
        suser.setBalance(suser.getBalance()-Double.parseDouble(price));
        userService.updateUser(suser);
        aa.put("code",0);
        aa.put("requestdata",suser);
        aa.put("msg","核销成功,"+suser.getNickName()+"你本次消费金额为"+Double.parseDouble(price)+"元");
        map.put("data",aa);
        return map;

    }
    @PostMapping("orderuserinfo")
    HashMap<Object, Object> orderuserinfo(@RequestBody SysUser sysUser) {
        HashMap<Object, Object> map = new HashMap<>();
        String imageType = "PNG";
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
        String newsNo = LocalDateTime.now().format(fmt);
        String ewmInfo = ewmUtils.generateBase64("orderid={\"myuserinfo\":{\"userName\":\""+sysUser.getUserName()+"\"," +
                "\"nickName\":\""+sysUser.getNickName()+"\"," +
                "\"phonenumber\":\""+sysUser.getPhonenumber()+"\",\"qrcodeid\":\""+newsNo+"\"}}", imageType);

        map.put("qrcode",ewmInfo);
        map.put("qrcodeid",newsNo);
        return map;
    }
    @PostMapping("/handleRecharge")
    public HashMap<Object, Object> handleRecharge(@RequestBody Map parammap)
    {    HashMap<Object, Object> map = new HashMap<>();
        JSONArray userids = JSONObject.parseArray(parammap.get("userids").toString());
        String type = parammap.get("type").toString();
        String balance = parammap.get("balance").toString();
        if(type.equals("cz")){
            type="充值";
        }else if(type.equals("bt")){
            type="补贴";
        }else{

        }

        StVerificationQrCode stVerificationQrCode = new StVerificationQrCode();
        for(int i=0;i<userids.size();i++){
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
            String newsNo = LocalDateTime.now().format(fmt);
            SysUser suser = userService.selectUserById((long) Integer.parseInt(userids.get(i).toString()));
            stVerificationQrCode.setId(newsNo);
            stVerificationQrCode.setName(type);
            stVerificationQrCode.setUser(suser.getNickName());
            stVerificationQrCode.setUserName(suser.getUserName());
            stVerificationQrCode.setPhone(suser.getPhonenumber());
            stVerificationQrCode.setVerificationTime(DateUtils.getTime());
            stVerificationQrCode.setPrice(balance);
            stVerificationQrCode.setStates("核销成功");
            stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
            /*充值*/
            StUserOpe stUserOpe=new StUserOpe();
            stUserOpe.setUserName(suser.getUserName());
            stUserOpe.setBalance(suser.getBalance()+"");
            stUserOpe.setName(type);
            stUserOpe.setPhone(suser.getPhonenumber());
            stUserOpe.setOpetime(DateUtils.getTime());
            stUserOpeService.insertStUserOpe(stUserOpe);
            suser.setBalance(suser.getBalance()+Double.parseDouble(balance));
            userService.updateUser(suser);
        }
        map.put("code",0);
        map.put("msg","充值成功");
        return map;
    }
    @PostMapping("getuserinfo")
    HashMap<Object, Object> getuserinfo(@RequestBody SysUser sysUser) {
        HashMap<Object, Object> map = new HashMap<>();
        SysUser suser = userService.selectUserByPhone(sysUser.getPhonenumber());
        map.put("sysUserinfo",suser);
        return map;
    }
    @PostMapping("/statisticsapp")
    public HashMap<Object, Object> statisticsapp(@RequestBody StVerificationQrCodestatistics stVerificationQrCodestatistics)
    {
        HashMap<Object, Object> map = new HashMap<>();
        List<StVerificationQrCode> list = stVerificationQrCodeService.selectStVerificationQrCodeListstatistics(stVerificationQrCodestatistics);
        map.put("data",list);
        return map;
    }
    @PostMapping("/cookedorder")
    public HashMap<Object, Object> cookedorder(@RequestBody JSONArray parammap)
    {
        HashMap<Object, Object> map = new HashMap<>();
           String ids="";
           String foodcounts="";
           String  username="";
           Double  totalAmount=0.0;
           String[] idsArray=new String[parammap.size()];
           String[] foodcountsArray = new String[parammap.size()];
          String foodImageurl="";
            for (int i=0;i<parammap.size();i++){
                StOrderDetail stOrderDetail =  new StOrderDetail();

                JSONObject maps= JSON.parseObject(JSON.toJSONString(parammap.get(i)));
                stOrderDetail.setFoodname(maps.getString("foodname"));
                stOrderDetail.setFoodcount(maps.getString("foodprice"));
                stOrderDetail.setFoodprice(maps.getString("foodcount"));
                stOrderDetail.setUsername(maps.getString("username"));
                stOrderDetailService.insertStOrderDetail(stOrderDetail);
                /*更新库存*/
                StFood stFood = stFoodService.selectStFoodById(maps.getString("id"));
                foodImageurl=stFood.getFoodimage();
                stFood.setStock((Double.parseDouble(stFood.getStock())-Double.parseDouble(maps.getString("foodcount")))+"");
                stFoodService.updateStFood(stFood);
                foodcountsArray[i]=maps.getString("foodcount");
                idsArray[i]=stOrderDetail.getId()+"";
                username=maps.getString("username");
                totalAmount=totalAmount+Double.parseDouble(maps.getString("foodprice"))*Double.parseDouble(maps.getString("foodcount"));
            }

        ids = String.join(",", idsArray);
        foodcounts = String.join(",", foodcountsArray);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
        String newsNo = LocalDateTime.now().format(fmt);
        StOrder stOrder=new StOrder();
        stOrder.setId(newsNo);
        stOrder.setOrderids(ids);
        stOrder.setName("熟食");
        DateTimeFormatter ftt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String time = LocalDateTime.now().format(ftt);
        stOrder.setTime(time);
        stOrder.setFoodcounts(foodcounts);
        SysUser sysUser = userService.selectUserByUserName(username);
        stOrder.setUser(sysUser.getUserName());
        stOrder.setUsername(sysUser.getNickName());
        stOrder.setUserphone(sysUser.getPhonenumber());
        stOrder.setStates("1");
        stOrder.setImageurl(foodImageurl);
        stOrder.setPickupcode(newsNo);
        stOrder.setAmount(totalAmount+"");
        String imageType = "PNG";
        // 调用生成二维码方法
        String ewmInfo = ewmUtils.generateBase64("mySSorderid="+newsNo, imageType);
        stOrder.setQRcode(ewmInfo);
        Double YYtotalAmount = stOrderService.selectcountamountStOrder(sysUser.getUserName());
        if(sysUser.getBalance()<(totalAmount+YYtotalAmount)){
            map.put("code",-1);
            map.put("msg","余额不够，订购失败");
            return map;
        }
        //熟食预定直接扣钱
        //sysUser.setBalance(sysUser.getBalance()-totalAmount);
        userService.updateUser(sysUser);
        stOrderService.insertStOrder(stOrder);
        map.put("code",0);
        map.put("msg","订购成功");
        return map;
    }
    @PostMapping("/refundapp")
    public HashMap<Object, Object> refundapp(@RequestBody StVerificationQrCode stVerificationQrCode)
    {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        if(stVerificationQrCode.getStates().equals("核销成功")){
        stVerificationQrCode.setStates("退款申请中");
        }else{
            aa.put("code",-1);
            aa.put("msg","该订单不能退款");
            map.put("data",aa);
            return map;
        }
        stVerificationQrCodeService.updateStVerificationQrCode(stVerificationQrCode);
        aa.put("code",0);
        aa.put("msg","退款申请成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("/refundbyid")
    public HashMap<Object, Object> refundbyid(@RequestBody StVerificationQrCode stVerificationQrCode)
    {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        if(stVerificationQrCode.getStates().equals("退款申请中")){
        stVerificationQrCode.setStates("退款成功");
        SysUser sysUser = userService.selectUserByUserName(stVerificationQrCode.getUserName());
        if(stVerificationQrCode.getName().equals("充值")||stVerificationQrCode.getName().equals("补贴")){
            //充值补贴错误扣回金额
            sysUser.setBalance(sysUser.getBalance()-Double.parseDouble(stVerificationQrCode.getPrice()));
        }else{
            sysUser.setBalance(sysUser.getBalance()+Double.parseDouble(stVerificationQrCode.getPrice()));
        }
        userService.updateUser(sysUser);
        stVerificationQrCodeService.updateStVerificationQrCode(stVerificationQrCode);
        }else{
            aa.put("code",-1);
            aa.put("msg","退款失败,该订单不能退款");
            map.put("data",aa);
            return map;
        }
        aa.put("code",0);
        aa.put("msg","退款成功");
        map.put("data",aa);
        return map;
    }
    @PostMapping("/storderdetailbyid")
    public HashMap<Object, Object> storderdetailbyid(@RequestBody StOrder StOrder)
    {
        HashMap<Object, Object> map = new HashMap<>();

        List<StOrderDetail> aa = stOrderService.selectStOrderdetailListbyids(StOrder.getOrderids().split(","));
        map.put("data", aa);
        return map;
    }
    @PostMapping("Verification_Icard_userinfo_android")
    HashMap<Object, Object> Verification_Icard_userinfo_android(@RequestBody Map parammap ,String price,String serial ) {
        HashMap<Object, Object> map = new HashMap<>();
        HashMap<Object, Object> aa = new HashMap<>();
        System.out.println(">>>>>>"+price);
        if(serial==null){
            serial="";
        }
        System.out.println(">>>>>>设备序列号"+serial);


        String icard = parammap.get("icard").toString();
        System.out.println(">>>>>>该卡号"+icard);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//设置日期格式
        String newsNo = LocalDateTime.now().format(fmt);
        /*判断当前时间是否属于就餐时间*/
        StSysDic stSysDic=new StSysDic();
        List<StSysDic> stSysDiclist = stSysDicService.selectStSysDicList(stSysDic);
        boolean falg = false;
        System.out.println(">>>>>>>>>>>>>>>"+newsNo.substring(8,14));
        for(int i=0;i<stSysDiclist.size();i++){
            if(newsNo.substring(8,14).compareTo(stSysDiclist.get(i).getStarttime())>0&&newsNo.substring(8,14).compareTo(stSysDiclist.get(i).getEndtime())<0){
                falg=true;
            }
        }
        if(!falg){
            aa.put("code",-1);
            aa.put("msg","当前不属于就餐时间，请稍后再来");
            map.put("data",aa);
            return map;
        }

        StVerificationQrCode stVerificationQrCode = new StVerificationQrCode();
        stVerificationQrCode.setSerial(serial);
        SysUser suser = userService.selectUserByicard(icard);
        if(suser==null){
            aa.put("code",-1);
            aa.put("msg","该饭卡未激活或已注销，请找食堂管理员确认");
            map.put("data",aa);
            return map;
        }
        stVerificationQrCode.setPhone(suser.getPhonenumber());
        stVerificationQrCode.setUserName(suser.getUserName());
        stVerificationQrCode.setUser(suser.getNickName());
        stVerificationQrCode.setVerificationTime(DateUtils.getTime());
        stVerificationQrCode.setId(newsNo);
        stVerificationQrCode.setStates("核销成功");
        /*查询该用户今日是否已用餐*/
        StVerificationQrCode userinfoVerificationQrCode = new StVerificationQrCode();
        userinfoVerificationQrCode.setUserName(suser.getUserName());

        if(newsNo.substring(8,14).compareTo(stSysDicService.selectStSysDicById(1L).getStarttime())>0&&newsNo.substring(8,14).compareTo(stSysDicService.selectStSysDicById(1L).getEndtime())<0){
            System.out.println(">>>>>>>>>>>>>>>早餐核销"+newsNo.substring(8,14));
            stVerificationQrCode.setName("早餐核销");
        }
        if(newsNo.substring(8,14).compareTo(stSysDicService.selectStSysDicById(2L).getStarttime())>0&&newsNo.substring(8,14).compareTo(stSysDicService.selectStSysDicById(2L).getEndtime())<0){
            System.out.println(">>>>>>>>>>>>>>>午餐核销"+newsNo.substring(8,14));
            stVerificationQrCode.setName("午餐核销");
            /*查询今日是否已就餐*/
            userinfoVerificationQrCode.setName("午餐核销");
            userinfoVerificationQrCode.setVerificationTime(DateUtils.getTime().substring(0,10));
            System.out.println(">>>>>>>>>>>>>>>午餐核销"+DateUtils.getTime().substring(0,10));
            List<StVerificationQrCode> userinfoVerificationQrCodeList = stVerificationQrCodeService.selectStVerificationQrCodeList(userinfoVerificationQrCode);
            if(userinfoVerificationQrCodeList.size()==0){
                price="5.0";
            }else{
                price="15.0";
            }
        }
        if(newsNo.substring(8,14).compareTo(stSysDicService.selectStSysDicById(3L).getStarttime())>0&&newsNo.substring(8,14).compareTo(stSysDicService.selectStSysDicById(3L).getEndtime())<0){
            System.out.println(">>>>>>>>>>>>>>>晚餐核销"+newsNo.substring(8,14));
            stVerificationQrCode.setName("晚餐核销");
            /*查询今日是否已就餐*/
            userinfoVerificationQrCode.setName("晚餐核销");
            userinfoVerificationQrCode.setVerificationTime(DateUtils.getTime().substring(0,10));
            List<StVerificationQrCode> userinfoVerificationQrCodeList = stVerificationQrCodeService.selectStVerificationQrCodeList(userinfoVerificationQrCode);
            if(userinfoVerificationQrCodeList.size()==0){
                price="5.0";
            }else{
                price="10.0";
            }


        }
        if(newsNo.substring(8,14).compareTo(stSysDicService.selectStSysDicById(4L).getStarttime())>0&&newsNo.substring(8,14).compareTo(stSysDicService.selectStSysDicById(4L).getEndtime())<0){
            System.out.println(">>>>>>>>>>>>>>>宵夜核销"+newsNo.substring(8,14));
            stVerificationQrCode.setName("宵夜核销");
        }
        stVerificationQrCode.setPrice(price);
        if(Double.parseDouble(price)==0){
            stVerificationQrCode.setStates("刷卡失败,消费金额为0.0");
            stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
            aa.put("code",-1);
            aa.put("msg","刷卡失败,消费金额为0.0");
            map.put("data",aa);
            return map;
        }else if(suser.getBalance()<Double.parseDouble(price)){
            stVerificationQrCode.setStates("刷卡失败,余额不够");
            stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
            aa.put("code",-1);
            aa.put("msg","余额不够，付款失败");
            map.put("data",aa);
            return map;
        }
        stVerificationQrCodeService.insertStVerificationQrCode(stVerificationQrCode);
        /*扣款*/
        suser.setBalance(suser.getBalance()-Double.parseDouble(price));
        userService.updateUser(suser);
        aa.put("code",0);
        aa.put("requestdata",suser);
        aa.put("msg","刷卡成功,"+suser.getNickName()+"你本次消费金额为"+Double.parseDouble(price)+"元");
        map.put("data",aa);
        return map;

    }

}
